<?php
/**
 * @Name  : UserModel.php
 * @Author: yashuai<1762910894@qq.com>
 */

namespace App\Http\Model\Rbac;

use App\Http\Model\BaseModel;
use HugCode\PhpUnits\Frame\Code\ConstCode;

class UserModel extends BaseModel
{

    protected $primaryKey = 'UserID';

    protected $table      = 'admin_user';

    /**
     * 关联用户角色
     */
    public function role()
    {
        return $this->hasMany(UserRoleModel::class, 'UserID', 'UserID')
            ->select(['UserID', 'RoleID']);
    }

    /**
     * @Desc   验证用户是否存在
     * @param  int     $userId
     * @param  bool    $status  是否验证状态
     * @param  string  $select
     * @return array|\Illuminate\Database\Query\Builder|mixed
     * @throws \App\Exceptions\ThrowException
     * @author yashuai<1762910894@qq.com>
     */
    public static function userExist($userId = 0, $status = false, $select = '*')
    {
        $info = self::getInfo($userId, 'UserID', $select);
        if (empty($info) || ($status && $info['UserStatus'] != ConstCode::BASIC_STATUS_NORMAL)) {
            _throw('', 106001);
        }

        return $info;
    }

}
